package com.wkck.controller.admin;

import com.wkck.VO.NewsInfoVO;
import com.wkck.VO.PageResult;
import com.wkck.VO.UploadContentVo;
import com.wkck.domain.News;
import com.wkck.domain.NewsContent;
import com.wkck.dto.NewsQueryBackendDto;
import com.wkck.dto.SaveNewsInfoDto;
import com.wkck.result.Result;
import com.wkck.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: CK
 * @Date: 2024/02/15/23:03
 * @Description:
 */
@Api(tags = "新闻管理",value = "新闻管理")
@Slf4j
@RequestMapping("/newsBg")
@RestController
@CrossOrigin
public class NewsBgController {

    @Autowired
    private NewsService newsService;

    /***
    * @Author: CK
    * @Date: 2024/3/21
    * @Param: [dto]
    * @Description: 得到新闻列表数据
    */
    @PostMapping("/get")
    @ApiOperation(value = "得到新闻列表数据")
    public Result<PageResult> getNewsInfo(@RequestBody NewsQueryBackendDto dto) {
        log.info("得到新闻列表数据: {}", dto);
        return Result.success(newsService.getNewsInfo(dto));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/21
    * @Param: [id]
    * @Description: 根据新闻id得到新闻信息
    */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据新闻id得到新闻信息")
    public Result<NewsInfoVO> getNewsInfoById(@PathVariable Long id) {
        log.info("需要获取详情的id ====> {}", id);
        return Result.success(newsService.getNewsInfoById(id));
    }

    /***
     * @Author: CK
     * @Date: 2024/3/21
     * @Param: []
     * @Description: 创建新闻数据记录
     */
    @GetMapping("/save")
    @ApiOperation(value = "创建新闻数据记录")
    public Result<String> createNewsInfo() {
        log.info("创建新闻数据记录");
        return Result.success(newsService.createNewsInfo());
    }

    /***
    * @Author: CK
    * @Date: 2024/3/22
    * @Param: [file, id]
    * @Description: 新闻添加封面图
    */
    @PostMapping("/title-photo/{id}")
    @ApiOperation(value = "新闻添加封面图")
    public Result<String> insertNewsTitlePhotoById(@RequestPart MultipartFile file,  @PathVariable Long id) throws IOException {
        log.info("需要添加的封面图的新闻id ===> {}", id);

        File tempFile = File.createTempFile("minio_news_title_photo", ".temp");
        file.transferTo(tempFile);

        String localFilePath = tempFile.getAbsolutePath();

        long size = file.getSize();

        String filename = file.getOriginalFilename();

        return Result.success(newsService.insertNewsTitlePhotoById(localFilePath,size,filename,id));
    }

    /**
    * @Author: CK
    * @Date: 2024/3/22
    * @Param: [id]
    * @Description: 删除新闻
    */
    @DeleteMapping("/remove")
    @ApiOperation(value = "删除新闻")
    public Result<Boolean> removeNewsById(@RequestBody Map<String, List<String>> ids) {
        log.info("需要删除的新闻id ===> {}", ids.get("ids"));
        return Result.success(newsService.removeNewsById(ids.get("ids")));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/22
    * @Param: [file, id]
    * @Description: 上传正文中图片
    */
    @PostMapping("/content-photo/{id}")
    @ApiOperation(value = "上传正文中图片")
    public Result<UploadContentVo> uploadContentPhoto(@RequestPart MultipartFile file, @PathVariable("id") Long id) throws IOException {
        log.info("需要上传的正文 ===> {}, 图片: {}", id, file);

        File tempFile = File.createTempFile("minio_news_content_photo", ".temp");
        file.transferTo(tempFile);

        String localFilePath = tempFile.getAbsolutePath();

        long size = file.getSize();

        String filename = file.getOriginalFilename();

        return Result.success(newsService.uploadContentPhoto(localFilePath, size, filename, id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/23
    * @Param: [dto]
    * @Description: 保存新闻信息
    */
    @PostMapping("/save-content")
    @ApiOperation(value = "保存新闻信息")
    public Result<Boolean> saveNewsInfo(@RequestBody SaveNewsInfoDto dto){
        log.info("需要保存的新闻信息 ====> {}", dto);
        return Result.success(newsService.saveNewsInfo(dto));
    }

    /****
    * @Author: CK
    * @Date: 2024/3/23
    * @Param: [news]
    * @Description: 修改新闻状态
    */
    @PutMapping("/change-state")
    @ApiOperation(value = "修改新闻状态")
    public Result<Boolean> changeState(@RequestBody News news) {
        log.info("需要修改新闻的状态 : {}", news);
        return Result.success(newsService.changeState(news));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/23
    * @Param: [id]
    * @Description: 预览新闻
    */
    @GetMapping("/preview-content/{id}")
    @ApiOperation(value = "预览新闻")
    public Result<NewsContent> getPreviewNewsContent(@PathVariable Long id) {
        log.info("预览新闻id ===> {}", id);
        return Result.success(newsService.getPreviewNewsContent(id));
    }
}
